package com.charles.seckillManagement.controller;

import com.charles.seckillComponentCommon.controller.BaseController;
import com.charles.seckillComponentCommon.domain.AjaxResult;
import com.charles.seckillComponentCommon.domain.R;
import com.charles.seckillComponentCommon.domain.TableInfo;
import com.charles.seckillComponentSecurity.annotation.RolePermit;
import com.charles.seckillComponentSecurity.utils.ThreadUtil;
import com.charles.seckillData.domain.SysUser;
import com.charles.seckillData.model.UserLogin;
import com.charles.seckillData.vo.LoginInfo;
import com.charles.seckillManagement.form.UserListQuerys;
import com.charles.seckillManagement.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin/user")
public class AdminUserController extends BaseController {
    
    @Autowired
    private SysUserMapper sysUserMapper;

    /**
     * 通过帐号获取管理员信息
     */
    @GetMapping("/data/{user_card}")
    public R<UserLogin> userInfo(@PathVariable("user_card") String user_card) {
        SysUser sysUser = sysUserMapper.queryUserByUserCard(user_card);
        if (sysUser == null) {
            return R.fail("帐号或密码错误");
        }
        UserLogin userLogin = new UserLogin();
        userLogin.setSysUser(sysUser);
        return R.success(userLogin);
    }

    /**
     * 获取当前用户
     */
    @GetMapping("getUser")
    public AjaxResult getUser() {
        String userId = ThreadUtil.getUserId();
        LoginInfo user = sysUserMapper.queryLoginInfoById(Long.parseLong(userId));
        return AjaxResult.success(user);
    }

    /**
     * 客户列表
     */
    @GetMapping("getUserList")
    @RolePermit(values = "admin")
    public TableInfo getUserList(UserListQuerys userListQuerys) {
        startPage();
        return getDataTable(sysUserMapper.queryUserTableList(userListQuerys));
    }

    /**
     * 删除客户
     */
    @DeleteMapping("/deleteUser/{userId}")
    @RolePermit(values = "admin")
    public void deleteUser(@PathVariable("userId") Long userId) {
        sysUserMapper.deleteUser(userId);
    }
}
